package cn.rulian.base.basis.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.rulian.base.basis.bo.EnumDetail;
import cn.rulian.base.basis.service.EnumDetailService;
import cn.rulian.base.basis.vo.EnumDetailVo;
import cn.rulian.base.common.dto.Message;
import cn.rulian.base.common.vo.Result;

/**
 * 公共类型
 */

@Controller
@RequestMapping("/base/basis/enumDetail")
public class EnumDetailController {
	
	@Autowired
	private EnumDetailService service;
	
	private final String path="base/basis/enumDetail_"; //页面基本路径

	/**
	 * 页面基本路径
	 */
	private String getPath()
	{
		return path;
	}
	
	
	/**
	 * 列表页面
	 */
	@RequestMapping("/listPage")
	public String listPage(Model model,String key) {

		model.addAttribute("key", key); //类型key
		return this.getPath()+"list";
	}

	/**
	 * 查询
	 */
	@RequestMapping("/listData")
	@ResponseBody
	public Result listData(EnumDetailVo vo) {
		return service.query(vo);
	}


	/**
	 * 增加页面
	 */
	@RequestMapping("/addPage")
	public String addPage(Model model,String key) {
		model.addAttribute("key", key); //类型key
		return this.getPath()+"add";
	}

	/**
	 * 增加
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Message add(EnumDetailVo vo) {
		Message rm = new Message();
		try {
			EnumDetail bo = vo.getObj();
			service.save(bo);
			rm.setMsg("保存成功！");
			rm.setCode(Message._SUCCESS);
		} catch (Exception e) {
			rm.setMsg("保存失败，异常信息：<br/>" + e.toString());
			rm.setCode(Message._ERROR);
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 修改页面
	 */
	@RequestMapping("/updatePage")
	public String updatePage(Model model, Long id) {
		EnumDetail bo = service.findOne(id);
		model.addAttribute("bo", bo); //类型
		return this.getPath()+"update";
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Message update(EnumDetailVo vo,Long id) {
		Message rm = new Message();
		try {
			EnumDetail obj = vo.getObj();
			EnumDetail bo = service.findOne(id);
			
			bo.setName(obj.getName());
			bo.setRemark(obj.getRemark());
			bo.setOrderNum(obj.getOrderNum());
			
			service.save(bo);
			
			rm.setMsg("保存成功！");
			rm.setCode(Message._SUCCESS);
		} catch (Exception e) {
			rm.setMsg("保存失败，异常信息：<br/>" + e.toString());
			rm.setCode(Message._ERROR);
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Message delete(Long ids[]) {
		Message rm = new Message();
		try {
			for(Long id:ids)
			{
				service.delete(id);
			}
			
			rm.setMsg("删除成功！");
			rm.setCode(Message._SUCCESS);
		} catch (Exception e) {
			rm.setMsg("删除失败！！！<br/><br/>原因：该信息已被其使用，不能删除！" );
			rm.setCode(Message._ERROR);
			e.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 详情页面
	 */
	@RequestMapping("/detailPage")
	public String detailPage(Model model, Long id) {
		EnumDetail bo = service.findOne(id);
		model.addAttribute("bo", bo); //类型
		return this.getPath()+"detail";
	}
	

	/**
	 * 根据类型Key查询，主要供页面下拉框取数据用
	 */
	@RequestMapping("/findByKey")
	@ResponseBody
	public List<EnumDetail> findByKey(String key)
	{
		return service.findByKey(key);
	}

}
